package com.longdaji.decoration.ui.goodslist.select;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.data.bean.GoodsBean;
import com.longdaji.decoration.utils.ImageUtil;
import com.longdaji.decoration.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longtao on 2018/1/31.
 */

public class GoodsSelectAdapter extends RecyclerView.Adapter<GoodsSelectAdapter.ViewHolder> {
    private GoodsSelectItemListener mListener;
    private List<GoodsBean.Goods> mList;
    private Context mContext;

    public interface GoodsSelectItemListener {

        void onGoodsItemClick(GoodsBean.Goods goodItem);

        void onCollectClick(int position, String goodId);

        void onDisCollectClick(int position, String goodId);
    }

    //建立枚举 2个item 类型
    public enum ITEM_TYPE {
        ITEM1,
        ITEM2
    }

    public GoodsSelectAdapter(Context context, List<GoodsBean.Goods> list, GoodsSelectItemListener listener) {
        this.mContext = context;
        this.mListener = listener;
        if (list == null) {
            this.mList = new ArrayList<>();
        } else {
            this.mList = list;
        }

    }

    public void setData(List<GoodsBean.Goods> list) {
        if (list == null) {
            this.mList = new ArrayList<>();
        } else {
            this.mList = list;
        }

        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return ITEM_TYPE.ITEM1.ordinal();
        } else {
            return ITEM_TYPE.ITEM2.ordinal();
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == ITEM_TYPE.ITEM1.ordinal()) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_left, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_right, parent, false);
        }

        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final GoodsBean.Goods goodItem = mList.get(position);
        Log.d("GoodsSelectAdapter", "goodItem is " + goodItem);
        //GlideUtils.loadImage(mContext, goodItem.getImgAddress(), holder.goodsPic, null, R.drawable.no_banner, R.drawable.no_banner);
//        Glide.with(mContext)
//                .load(goodItem.getImgAddress())
//                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                .centerCrop()
//                //.placeholder(R.drawable.no_banner)
//                .error(R.drawable.no_banner)
//                .into(holder.goodsPic);
        //ImageUtil.loadIntoUseFitWidth(mContext, goodItem.getImgAddress(), R.drawable.no_banner, holder.goodsPic);
        ImageUtil.loadImage(mContext, goodItem.getImgAddress(), holder.goodsPic);
        holder.goodsName.setText(goodItem.getGoodsName());
        holder.goodsPrice.setText("¥" + StringUtil.formatNumber(goodItem.getGoodsDefaultPrice()));
        if (TextUtils.equals("1", goodItem.getIsCollection())) {
            holder.collect.setImageResource(R.mipmap.icon_collect);
        } else {
            holder.collect.setImageResource(R.mipmap.icon_uncollect);
        }
        holder.goodsItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onGoodsItemClick(goodItem);
                }
            }
        });
        holder.collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    if (TextUtils.equals("1", goodItem.getIsCollection())) {
                        mListener.onDisCollectClick(position, goodItem.getGoodsId());
                    } else {
                        mListener.onCollectClick(position, goodItem.getGoodsId());
                    }

                }
            }
        });
    }

    public GoodsBean.Goods getItemByPosition(int position) {
        return mList.get(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView goodsPrice;
        private TextView goodsName;
        private LinearLayout goodsItem;
        private ImageView goodsPic;
        private ImageView collect;

        public ViewHolder(View itemView) {
            super(itemView);
            goodsItem = itemView.findViewById(R.id.llyt_goods_item);
            goodsName = itemView.findViewById(R.id.tv_goods_name);
            goodsPrice = itemView.findViewById(R.id.tv_goods_price);
            goodsPic = itemView.findViewById(R.id.iv_goods_pic);
            collect = itemView.findViewById(R.id.iv_collect);
        }
    }

}
